<?php
/*
 * Created on Jan 24, 2010
 * Shopping Cart System
 * Owner: Phu Ha
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class Router {
	private $registry;
	private $path;
	private $args = array();
	private $logger;

	function __construct($registry) {
		$this->registry = $registry;
		$this->logger = Logger::getLogger(LOGGER_MVC);
		$this->logger->info('Initiate Router object');
	}       

	function setPath($path) {
		$this->logger->debug('Method setPath: $path: ' . $path);
		
        // $path = trim($path, '/\\');
        $path .= DIRSEP;
		
        if (is_dir($path) == false) {
			$this->logger->debug('Method setPath: $path is not directory ' . $path);
			throw new Exception ('Invalid controller path: `' . $path . '`');
        }

        $this->path = $path;
	}
	
	private function getController(&$file, &$controller, &$action, &$args) {
        $route = (empty($_GET['route'])) ? '' : $_GET['route'];

        if (empty($route)) {
			$this->logger->debug('Method getController: $route is empty, set it to default Controller');
			$route = DEFAULT_CONTROLLER;
		}
		
		// this default Controller should be declared as CONST of system to identify which Controller should be default
		// it depends on Application, if there is no default specified, then this DefaultController should be default
		// 

        // Get separate parts
        $route = trim($route, '/\\');
        $parts = explode('/', $route);

        // Find right controller
        $cmd_path = $this->path;
        foreach ($parts as $part) {
                $fullpath = $cmd_path . $part;
                       
                // Is there a dir with this path?
                if (is_dir($fullpath)) {
						$this->logger->debug('Method getController: $fullpath is directory ' . $fullpath);
                        $cmd_path .= $part . DIRSEP;
                        array_shift($parts);
                        continue;
                }

                // Find the file
                if (is_file($fullpath . '.php')) {
						$this->logger->debug('Method getController: $fullpath' . '.php' . ' is file ');
                        $controller = $part;
                        array_shift($parts);
                        break;
                }
        }

        if (empty($controller)) {
			$this->logger->debug('Method getController: $controller is empty then will be set to default ' . DEFAULT_CONTROLLER);
			$controller = DEFAULT_CONTROLLER;
		}

        // Get action
        $action = array_shift($parts);
        if (empty($action)) {
			$this->logger->debug('Method getController: $action is empty then will be set to default ' . DEFAULT_ACTION);
			$action = DEFAULT_ACTION;
		}

        $file = $cmd_path . $controller . '.php';
        $args = $parts;
	}

	function delegate() {
        // Analyze route
        $this->getController($file, $controller, $action, $args);
		
		$this->logger->debug('Method delegate: getController returns ');
		$this->logger->debug('Method delegate: $file: ' . $file);
		$this->logger->debug('Method delegate: $controller: ' . $controller);
		$this->logger->debug('Method delegate: $action: ' . $action);
		$this->logger->debug('Method delegate: $args: ' . $args);
		
        // File available?
        if (is_readable($file) == false) {
        	$this->logger->error($file . ' is not readable or doesn\'t exist');
        	die($file . ' is not readable or doesn\'t exist');
        }

        // Include the file
        require_once($file);

        // Initiate the class
        $class = $controller;
        $controller = new $class($this->registry); // (1)
	
        // Action available?
        if (is_callable(array($controller, $action)) == false) {
        	$this->logger->error($action . ' is not executable');	
        	die($action . ' is not executable');
        }
		
        // Run action
		$this->logger->info('Method delegate: Ready to execute Controller with specific Action and Arguments');
        $controller->$action($args); // (2)
	}
}
?>
